<template>
    <div class="app-container">
        <!-- 基础信息 -->
        <h5 class="titleInfo">
            推荐关系修改
        </h5>
        <el-form class="form" ref="form"  :rules="rules" :model="form" label-width="130px">
            <el-row>
                <el-col :span="24">
                    <el-form-item label="会员/手机号">
                        <el-input v-model="searchPhone"></el-input>
                        <el-button style="margin-top:10px" type="primary" @click="searchPhoneFn()" >查询</el-button>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="原上级姓名">
                        <el-input readonly v-model="searchForm.realname"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="原上级手机">
                        <el-input readonly v-model="searchForm.phone"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="新上级手机" prop="phone">
                        <el-input v-model="form.phone"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="更换类型" prop="type">
                        <el-select v-model="form.type" placeholder="请选择">
                            <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="修改备注" prop="remark">
                        <el-input v-model="form.remark"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-form-item>
                <el-button style="margin-top:30px" type="primary" @click="submitFn('form')" >立即提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>


import { policy } from '@/api/policy'
import { getParentByPhone,dealRelation } from '@/api/designerInfo'


export default {
    name:"designerInfoRelationEdit",
    data(){
        return {
            options:[{
                    value:"0",
                    label:"带团队"
                },{
                    value:"1",
                    label:"不带团队"
                }
            ],
            form:{
                userId:"",
                phone:"",
                type:"",
                remark:"",
            },
            searchPhone:'',
            searchForm:{
                phone:'',
                realName:''
            },
            rules: {
                phone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' }
                ],
                type: [
                    { required: true, message: '请选择更换类型', trigger: 'blur' }
                ],
                remark: [
                    { required: true, message: '请输入备注', trigger: 'blur' }
                ]
            },
        }
    },
    inject: ['reload'],
    methods:{
        //查询手机号
        searchPhoneFn(){
            let datas = {
                id:this.searchPhone
            }
            getParentByPhone(datas).then(res=>{
                console.log(res)
                if(!res.data.code){
                    this.searchForm = res.data.data
                    this.form.userId = res.data.data.selfId
                }else{
                    this.$message.error(res.data.msg);
                }
            })
        },
        // 修改提交
        submitFn(e){
            this.$refs[e].validate((valid) => {
                if (valid) {
                    let datas = this.form
                    if(this.form.userId==""){
                        this.$message({
                            message: '没有选择用户',
                            type: 'warning'
                        });
                    }else{
                        // console.log(datas)
                        dealRelation(datas).then(res=>{
                            console.log(res)
                            if(!res.data.code){
                                this.$message({
                                    showClose: true,
                                    message: '添加成功',
                                    type: 'success'
                                });
                                this.$router.push('/designerInfoRelation')
                                this.reload()
                            }else{
                                this.$message({
                                    showClose: true,
                                    message: res.data.msg,
                                    type: 'warning'
                                });
                            }
                        })
                        .catch(error =>{
                            this.$message.error(error);
                        })
                    }
                }else{
                    this.$message({
                        message: '必填项不能为空',
                        type: 'warning'
                    });
                    return false;
                }
            });
        }
    }
}
</script>
<style scoped>
    .app-container{padding: 0.1px!important}
</style>